package com.ebt.m.account;

import com.ebt.m.AppInitService;
import com.ebt.m.account.json.AccountResponse;
import com.ebt.m.account.json.BindPhoneRequest;
import com.ebt.m.account.json.ChangePasswordRequest;
import com.ebt.m.account.json.ResetPasswordRequest;
import com.ebt.m.account.json.UserInfoRequest;
import com.ebt.m.account.json.ValidateCodeCheckRequest;
import com.ebt.m.account.json.ValidateCodeCheckResponse;
import com.ebt.m.account.json.ValidateCodeSendRequest;
import com.ebt.m.account.json.ValidateCodeSendResponse;
import com.ebt.m.commons.buscomponent.listview.BaseIView;
import com.ebt.m.commons.utils.RxUtil;
import com.ebt.m.data.bean.UserInfo;
import com.ebt.m.data.rxModel.api.EBTAPI;
import com.ebt.m.commons.model.data.BaseDataResult;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Rick.Wang on 2016/10/22.
 */
public class AccountHttps {

    private EBTAPI mApi;

    private static AccountHttps instance;

    private AccountHttps() {
        mApi = AppInitService.getEbtApi();
    }

    public static AccountHttps getInstance() {
        if (instance == null) {
            synchronized (AccountHttps.class) {
                if (instance == null) {
                    instance = new AccountHttps();
                }
            }
        }
        return instance;
    }

    //检查手机号是否已经被绑定
    public void checkPhoneNumberIfBound(String phoneNumber, String token, Observer<BaseDataResult<AccountResponse>> subscriber) {
        mApi.checkPhoneNumberIfBound(phoneNumber, token)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    //绑定新手机号，或者更改绑定的手机号
    public void bindPhoneNumber(String phoneNumber, String token, Observer<BaseDataResult<AccountResponse>> subscriber) {
        BindPhoneRequest bindPhoneRequest = new BindPhoneRequest(phoneNumber);
        mApi.bindPhoneNumber(bindPhoneRequest, token)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    //更改密码
    public void changeAccountPassword(String userId, String newPassword, String oldPassword,
                                      Observer<BaseDataResult<AccountResponse>> subscriber) {
        ChangePasswordRequest request = new ChangePasswordRequest(userId, newPassword, oldPassword);
        mApi.changeAccountPassword(request)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    //重置密码，暂时之后第三方登录，并且已经绑定手机号后才传入绑定的手机号，未验证
    public void resetAccountPassword(String phoneNumber, String newPassword,
                                     Observer<BaseDataResult<AccountResponse>> subscriber) {
        ResetPasswordRequest request = new ResetPasswordRequest(phoneNumber, newPassword);
        mApi.resetAccountPassword(request)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    //前台发送手机验证码
    public void sendValidateCode(String phoneNumberToReceiveVCode, Observer<BaseDataResult<ValidateCodeSendResponse>> subscriber, BaseIView iView) {
        ValidateCodeSendRequest request = new ValidateCodeSendRequest(phoneNumberToReceiveVCode);
        mApi.sendValidateCode(request)
                .compose(RxUtil.rxTransformer(iView))
                .subscribe(subscriber);
    }

    //前台手机验证码验证
    public void checkValidateCode(String phone, String validateId, String validateCode,
                                  Observer<BaseDataResult<ValidateCodeCheckResponse>> subscriber) {
        ValidateCodeCheckRequest request = new ValidateCodeCheckRequest(phone, validateId, validateCode);
        mApi.checkValidateCode(request)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getUserInfoByUserId(String getType, String requestValue, Observer<BaseDataResult<UserInfo>> subscriber) {
        UserInfoRequest request = new UserInfoRequest(getType, requestValue);
        mApi.getUserInfoByUserId(request)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }
}